final <- final_df
final <- final %>% mutate(days_squared = sign(days)*days^2)
final <- final %>% mutate(days_cubed = days^3)

#determine bandwidth
y_violence <- final$violence
y_norms <- final$norms
y_affpol <- final$affpol
x_1 <- final$days
weights <- final$weights

rdrobust(y_violence, x_1, c = 0)
rdrobust(y_norms, x_1, c = 0)
rdrobust(y_affpol, x_1, c = 0)

stan <- final %>% 
  mutate(violence = scale(violence),
         norms = scale(norms),
         affpol = scale(affpol),
         pvi_state = scale(pvi_state))

stan_violence <- stan %>% filter(days >= -43 & days <= 43) %>% mutate(days_scaled = as.numeric(scale(days, center = F)))
stan_norms <- stan %>% filter(days >= -33 & days <= 33) %>% mutate(days_scaled = as.numeric(scale(days, center = F)))
stan_affpol <- stan %>% filter(days >= -37 & days <= 37) %>% mutate(days_scaled = as.numeric(scale(days, center = F)))


gov_races <- c('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Iowa', 'Kansas', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Nebraska', 'Nevada', 'New Hampshire', 'New Mexico', 'New York', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Vermont', 'Wisconsin', 'Wyoming') %>% toupper()

govrace_codes <- statecode %>% filter(state %in% gov_races)

senate_races <- c('Alabama',  'Alaska',  'Arizona' , 'Arkansas',  'California' , 'Colorado' , 'Connecticut',  'Florida' , 'Georgia' , 'Hawaii'  ,'Idaho' , 'Illinois' , 'Indiana' , 'Iowa' , 'Kansas' , 'Kentucky' , 'Louisiana',  'Maryland' , 'Missouri' , 'Nevada' , 'New Hampshire' , 'New York',  'North Carolina' , 'North Dakota','Ohio',  'Oklahoma',  'Oregon' , 'Pennsylvania' , 'South Carolina',  'South Dakota'  ,'Utah'  ,'Vermont',  'Washington' , 'Wisconsin') %>% toupper()

senraces_codes <- statecode %>% filter(state %in% senate_races) 

bothracesA <- govrace_codes %>% inner_join(senraces_codes) %>% select(state_code) %>% as.vector() %>% unlist()
bothraces1 <- govrace_codes %>% inner_join(senraces_codes)

eitherraceA <- govrace_codes %>% full_join(senraces_codes) %>% anti_join(bothraces1) %>% select(state_code) %>% as.vector() %>% unlist()
eitherrace1 <- govrace_codes %>% full_join(senraces_codes) 

neitherraceA <- statecode %>% anti_join(bothraces1) %>% anti_join(eitherrace1) %>% select(state_code) %>% as.vector() %>% unlist()

bothraces <- final %>% filter(inputstate %in% as.numeric(bothracesA)) %>% mutate(which = 2)
eitherrace <- final %>% filter(inputstate %in% as.numeric(eitherraceA)) %>% mutate(which = 1)
neitherrace <- final %>% filter(inputstate %in% as.numeric(neitherraceA)) %>% mutate(which = 0)

races <- rbind(bothraces, eitherrace, neitherrace)

races$which <- factor(races$which)
races$which <- relevel(races$which, ref = '1')

stan_races <- races %>% 
  mutate(violence = scale(violence),
         norms = scale(norms),
         affpol = scale(affpol)) %>% 
  mutate(age = as.numeric(age))

stan_races_violence <- stan_races %>% filter(days >= -43 & days <= 43) %>% mutate(days_scaled = as.numeric(scale(days, center = F)))
stan_races_norms <- stan_races %>% filter(days >= -33 & days <= 33) %>% mutate(days_scaled = as.numeric(scale(days, center = F)))
stan_races_affpol <- stan_races %>% filter(days >= -37 & days <= 37) %>% mutate(days_scaled = as.numeric(scale(days, center = F)))